package com.example.document

import androidx.lifecycle.viewModelScope
import com.example.document.entity.DocEntity
import com.example.lib_base.BaseViewModel
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.onCompletion
import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.launch

class DocViewModel:BaseViewModel<DocRepository,DocState>() {
    override fun createState(): DocState {
        return DocState(false,false, mutableListOf(),false)
    }

    override fun createResp(): DocRepository {
        return DocRepository()
    }

    fun selDocuments(){
        viewModelScope.launch {
            val map = hashMapOf<String, Any>(Pair("userId", 1), Pair("page", 1))
            /*resp.selDocuments(map).catch {
                it.printStackTrace()
            }*/
            comFlow(resp.selDocuments(map))
                .collect{
                    val entity = it as DocEntity
                    mutableContainer.updateUiState { copy(docState=true,loadState=false,data=entity.data) }
                }
        }
    }

    fun delDocument(fileId:Int){
        val map = hashMapOf<String, Any>(Pair("fildId", fileId))
        viewModelScope.launch {
            comFlow(resp.delDocuments(map))
                .collect{
                    if(it.msg.equals("删除成功")){
                        mutableContainer.updateUiState {
                            copy(delState=true)
                        }
                        selDocuments()
                    }
                }
        }

    }
}